<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--学生各题型得分
class StudentProblemScore extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$grade = input("grade");
		$clazz = input("clazz");
		$examId = input("examId");
		$subject = input("subject");
		$studentNames = input("studentNames");
		$field = input("field");
		$page = input("page");
		$rows = input("rows");
		if(!$examId) return [];
		$subWhere["考试ID"] = $examId;
		$subWhere["学科"] = $subject;
		$where["转出学期ID"] = 0;
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		$subsql = Db::table('各题得分'.$grade)
				  ->where($subWhere)
				  ->buildSql();
		if($clazz) $where["a.班号"] = $clazz;
		if($studentNames) $where["姓名"] = array("in", explode(",", $studentNames));
		$fields = "";
		if($field) {
			$array = explode(",", $field);
			foreach ($array as $key => $value) {
				$fields = $fields.",".("题型".($key+1))." as '".$value."'";
			}
		}
		$result = Db::table('学生信息')->alias('a')
					->join([$subsql=>'b'],'a.ID=b.学生ID','LEFT')
					->field('a.班号,学号,姓名,学科,总分'.$fields)
					->where($where)
					->order("班号,学号")
					->page($page, $rows)
					->select();
		$count = Db::table("学生信息")->alias('a')
				  ->join([$subsql=>'b'],'a.ID=b.学生ID','LEFT')
				  ->where($where)
				  ->count();
		$grid["rows"] = selectResult($result);
		$grid["total"] = $count;
		return $grid;
	}

	public function subjectProblemScore() {
		$examId = input("examId");
		$subject = input("subject");
		$where["ID"] = $examId;
		$exam = Db::table("考试名称")
				->field("年级ID gradeId,考试名称 examName")
			    ->where($where)
			    ->find();
		$type = "期末";
		if(!strpos($exam["examName"],$type)){
			$type = "月测";
		}
		$twhere["年级"] = $exam["gradeId"];
		$twhere["学科"] = $subject;
		$data = Db::table("考试题型分值")
				->field($type." type")
				->where($twhere)
				->find();
		return $data["type"];
	}

	public function problemSubject(){
		$where["考试ID"] = input("examId");
		$grade = input("grade");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$result = Db::table("各题得分".$grade)
			   ->field("学科 subject")
			   ->distinct(true)
			   ->where($where)
			   ->order("subject")
			   ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$data[$key] = array("text"=>$obj["subject"],"value"=>$obj["subject"]);
		}
		return $data;
	}

	public function exportExcel() {
		$grade = input("grade");
		$clazz = input("clazz");
		$examId = input("examId");
		$subject = input("subject");
		$studentNames = input("studentNames");
		$field = input("field");
		$page = input("page");
		$rows = input("rows");
		if(!$examId) return [];
		$subWhere["考试ID"] = $examId;
		$subWhere["学科"] = $subject;
		$where["转出学期ID"] = 0;
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		$subsql = Db::table('各题得分'.$grade)
				  ->where($subWhere)
				  ->buildSql();
		if($clazz) $where["a.班号"] = $clazz;
		if($studentNames) $where["姓名"] = array("in", explode(",", $studentNames));
		$fields = "";
		$header = "班号,学号,姓名,学科,总分";
		if($field) {
			$array = explode(",", $field);
			foreach ($array as $key => $value) {
				$fields = $fields.",".("题型".($key+1))." as '".$value."'";
				$header .= ",".$value;
			}
		}
		$result = Db::table('学生信息')->alias('a')
					->join([$subsql=>'b'],'a.ID=b.学生ID','LEFT')
					->field('a.班号,学号,姓名,学科,总分'.$fields)
					->where($where)
					->order("班号,学号")
					->select();
		$result = selectResult($result);
		//excel表头内容
		$list = explode(",", $header);
		$header = [];
		foreach ($list as $key=> $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'学生各题型得分',$header);
		exit;
	}

}
